package com.minshenglife.zanzan.service.power.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.minshenglife.zanzan.dao.UserDao;
import com.minshenglife.zanzan.dao.power.EmpPowerDao;
import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.entity.WorkConPower;
import com.minshenglife.zanzan.entity.power.EmpPower;
import com.minshenglife.zanzan.service.power.EmpPowerService;

/**
 * 员工在不同日志类型下的权限service
 * @author 867101835
 *
 */
@Component
@Transactional
public class EmpPowerServiceImpl implements EmpPowerService {
	Logger log = LoggerFactory.getLogger(EmpPowerServiceImpl.class);

	@Resource
	EmpPowerDao empPowerDao;
	
	@Resource
	UserDao userDao;
	
	@Override
	public List<EmpPower> getEmpPowerList(String empId, String powerType,
			String type) {
		List<EmpPower> empPowerList = null;
		try{
			EmpPower power = new EmpPower();
			power.setEmpId(empId);
			power.setPower(powerType);
			power.setType(type);
			
			empPowerList = empPowerDao.getEmpPowerList(power);
			
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
		return empPowerList;
	}

	@Override
	public String save(String[] empIds, String power, String type) {
		String empIdTemp = "";
		try{
			if(empIds != null && empIds.length > 0){
				for (String empId : empIds) {
					if(empId != null && !empId.equals("")){
						empId = empId.trim();
						User user = new User();
						user.setEmpId(empId);
						List<User> users = userDao.findByExample(user);
						if(users != null && users.size() > 0){
							boolean flag = true;
							EmpPower empPower = new EmpPower();
							empPower.setEmpId(empId);
							empPower.setPower(power);
							empPower.setType(type);
							List<EmpPower> list = empPowerDao.getEmpPowerList(empPower);
							if(list != null && list.size() > 0){
								flag = false;
							}
							if(flag){
								empPowerDao.add(empPower);
							}
						}else{
							empIdTemp += empId+"，";
						}
					}
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
		return empIdTemp;
	}

	@Override
	public void delete(String empId, String power, String type) {
		try{
			EmpPower empPower = new EmpPower();
			empPower.setEmpId(empId);
			empPower.setPower(power);
			empPower.setType(type);
			empPowerDao.deleteEmpPower(empPower);
		}catch(Exception ex){
			ex.printStackTrace();
			return;
		}
		return;		
	}
	
}
